//
//  HPWebViewController.m
//  Beautiful Scenery Bar
//
//  Created by QianFeng on 15/10/23.
//  Copyright (c) 2015年 代恒彬. All rights reserved.
//

#import "HPWebViewController.h"
#import "AFNetworking.h"
@interface HPWebViewController ()<UIWebViewDelegate>{
    UIWebView*_myWebView;
    UIActivityIndicatorView *_active;
    UISegmentedControl *_seg;
}

@end

@implementation HPWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];
    [self createNavigationBar:9];
    [self.buttonA setImage:[UIImage imageNamed:@"collection-Small"] forState:UIControlStateNormal];
    self.back.text = @"发现详情";
    [self createSug];
}

-(void)createSug {
    _seg = [[UISegmentedControl alloc] initWithItems:@[@"返回", @"前进"]];
    _seg.frame = CGRectMake(ScreenWidth - 110, ScreenHeight - 38, 100, 30);
    _seg.momentary = YES;
    _seg.backgroundColor = RGBA(200, 200, 200, .5);
    _seg.tintColor = RGBA(44, 147, 253, .8);
    [_seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_seg];
}
- (void)segAction:(UISegmentedControl *)seg {
    if (seg.selectedSegmentIndex == 0) {
        [_myWebView goBack];
    } else {
        [_myWebView goForward];
    }
}
-(void)createView{
    _myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _myWebView.delegate =self;
    [self.view addSubview:_myWebView];
    [self loadHtmlPage];
    _active = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 15, ScreenHeight / 2 - 15, 30, 30)];
    _active.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    _active.hidesWhenStopped = YES;
    [self.view addSubview:_active];
}
-(void)loadHtmlPage{
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:_url]];
    [_myWebView loadRequest:request];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{

    [_active startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [_active stopAnimating];
}
-(void)initDataUrl:(NSString *)url{
    _url = url;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSMutableString * str = [NSMutableString stringWithString:[request.URL absoluteString]];
    if ([str hasPrefix:@"http://appview.qyer.com/place"] ||[str hasPrefix:@"http://appview.qyer.com/op"] || [str hasPrefix:@"http://m.qyer.com/pla"] || [str hasPrefix:@"http://m.qyer.com/bbs"]) {
        return YES;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"可能会跳出本站,进入不安全的链接哦" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
